package com.du.spring5.demo;

import com.du.spring5.Service.UserService;
import com.du.spring5.bean.Person;
import com.du.spring5.bean.Student;
import com.du.spring5.bean.User;
import com.du.spring5.bean.User1;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author : DuJiabao
 * @Date : 2021/2/3 10:45
 * @Project : spring
 * @File : null.java
 * @Desc :
 */
public class Demo1 {
  /**
   * 三种简单的注入方法
   */
  @Test
  public void test() {
//        bean工厂，导入xml的时候，默认是单例对象。
    ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//        通过set方法注入属性的方法
    User user = context.getBean("user", User.class);
//        通过有参构造器的注入属性方法
    User user1 = context.getBean("user1", User.class);
//        通过p名称空间注入属性的方法
    User user2 = context.getBean("user2", User.class);
    System.out.println(user);
    System.out.println(user1);
    System.out.println(user2);
//        单例对象
    System.out.println(context.getBean("user", User.class) == context.getBean("user", User.class)); //true
  }

  @Test
  public void test2() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
//        定义属性为空值的对象
    User user4 = context.getBean("user4", User.class);
    System.out.println(user4);
//      含有特殊字符的属性的对象
    User user5 = context.getBean("user5", User.class);
    System.out.println(user5);
//      含有特殊字符的属性的对象
    User user6 = context.getBean("user6", User.class);
    System.out.println(user6);
  }

  @Test
  public void test3() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
    UserService userService = context.getBean("userService", UserService.class);
    UserService userService2 = context.getBean("userService2", UserService.class);
    System.out.println(userService);
    System.out.println(userService2);
  }

  @Test
  public void test4() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
    Student student = context.getBean("student", Student.class);
    System.out.println(student);
    Student student2 = context.getBean("student2", Student.class);
    System.out.println(student2);
    User1 user1 = context.getBean("user1", User1.class);
    System.out.println(user1);
  }

  @Test
  public void test5() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
    User user1 = context.getBean("user1", User.class);
    User user1_2 = context.getBean("user1", User.class);
    System.out.println(user1);
    System.out.println(user1_2);
    System.out.println(user1 == user1_2);  // true

    User user2 = context.getBean("user2", User.class);
    User user2_2 = context.getBean("user2", User.class);
    System.out.println(user2);
    System.out.println(user2_2);
    System.out.println(user2_2 == user2);// true

    User user3 = context.getBean("user3", User.class);
    User user3_2 = context.getBean("user3", User.class);
    System.out.println(user3);
    System.out.println(user3_2);
    System.out.println(user3_2 == user3);// false
  }

  @Test
  public void test6() {
//        bean默认是单实例，一个bean就一个实例，但是可以修改的，修改成get一次，生成一个bean
    ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
//        prototype
    User user1 = context.getBean("user8", User.class);
    User user2 = context.getBean("user8", User.class);
    System.out.println(user2 == user1);  // false
//       singleton
    User user3 = context.getBean("user9", User.class);
    User user4 = context.getBean("user9", User.class);
    System.out.println(user3 == user4); // true
  }

  @Test
  public void test7() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean6.xml");
    System.out.println("get bean");
    Person person = context.getBean("person", Person.class);
    // 接口没有close，只能强转回ClassPathXmlApplicationContext
    ClassPathXmlApplicationContext context1 = (ClassPathXmlApplicationContext) context;
    context1.close();
  }

  @Test
  public void test8() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
    Student student = context.getBean("student", Student.class);
    System.out.println(student);
    Student student2 = context.getBean("student2", Student.class);
    System.out.println(student2);
  }

  @Test
  public void test9() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean9.xml");
    User user = context.getBean("user", User.class);
    User user1 = context.getBean("user1", User.class);
    System.out.println(user); // User{name='john', id=1}
    System.out.println(user1);  // User{name='john', id=2}
  }
}
